package article

import (
	"admin.supervise.limaochuxing.top/core"
	"admin.supervise.limaochuxing.top/core/request"
	"admin.supervise.limaochuxing.top/core/response"
	"admin.supervise.limaochuxing.top/middleware"
	"admin.supervise.limaochuxing.top/util"

	"admin.supervise.limaochuxing.top/admin/schemas/req"
	"admin.supervise.limaochuxing.top/admin/service/article"

	"github.com/gin-gonic/gin"
)

var ArticleCategoryGroup = core.Group("/", newCategoryHandler, regCategory, middleware.TokenAuth())

func newCategoryHandler(srv article.IArticleCategoryService) *categoryHandler {
	return &categoryHandler{srv: srv}
}

func regCategory(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *categoryHandler) {
		rg.GET("/article/cate/list", handle.list)
		rg.GET("/article/cate/detail", handle.detail)
		rg.POST("/article/cate/add", handle.add)
		rg.POST("/article/cate/edit", handle.edit)
		rg.POST("/article/cate/del", handle.del)
		rg.GET("/article/cate/all", handle.all) // 新增获取所有分类的路由
		rg.POST("/article/cate/change", handle.change)
	})
}

type categoryHandler struct {
	srv article.IArticleCategoryService
}

//list category列表
func (hd categoryHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.ArticleCategoryListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

//detail category详情
func (hd categoryHandler) detail(c *gin.Context) {
	var detailReq req.ArticleCategoryDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

//add category新增
func (hd categoryHandler) add(c *gin.Context) {
	var addReq req.ArticleCategoryAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(addReq))
}

//edit category编辑
func (hd categoryHandler) edit(c *gin.Context) {
	var editReq req.ArticleCategoryEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

//del category删除
func (hd categoryHandler) del(c *gin.Context) {
	var delReq req.ArticleCategoryDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.Id))
}

//all 获取所有文章分类
func (hd categoryHandler) all(c *gin.Context) {
	res, err := hd.srv.All()
	response.CheckAndRespWithData(c, res, err)
}

//change 改变文章分类状态
func (hd categoryHandler) change(c *gin.Context) {
	var changeReq req.ArticleCategoryChangeReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &changeReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Change(changeReq.Id))
}
